package com.zz.ecommerce.declareManage.common.service;

import com.zz.ecommerce.common.GridRequest;
import com.zz.ecommerce.common.GridResult;
import com.zz.ecommerce.common.bean.qdc.db.LogisticsOrderQdc;
import com.zz.ecommerce.common.bean.qdc.db.TGoodsListQdc;
import com.zz.ecommerce.common.bean.qdc.db.TOrderHeadQdc;
import com.zz.ecommerce.common.bean.qdc.db.TWaybillListQdc;
import com.zz.ecommerce.common.constants.TableConstants;
import com.zz.ecommerce.declareManage.common.dao.QdcGoodsListDao;
import com.zz.ecommerce.declareManage.common.dao.QdcOrderHeadDao;
import com.zz.ecommerce.declareManage.common.dao.QdcWaybillListDao;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.util.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by admin on 2016/3/29.
 */
@Service
public class QdcOrderService {

    @Autowired
    private QdcOrderHeadDao orderHeadDao;

    @Autowired
    private QdcGoodsListDao goodsListDao;

    @Autowired
    private QdcWaybillListDao waybillListDao;

    /**
     * 查询所有订单数据
     *
     * @param request
     * @param orderHead
     * @return
     */
    public GridResult queryOrderList(GridRequest request, TOrderHeadQdc orderHead) {
        int count = orderHeadDao.count(TableConstants.T_ORDER_HEAD_QDC, orderHead);
        List<TOrderHeadQdc> orderHeadList = orderHeadDao.page(TableConstants.T_ORDER_HEAD_QDC, orderHead,
                request.getStart(), request.getEnd(), request.getSortOrder());

        return new GridResult(orderHeadList, request, count);
    }

    /**
     * 查询所有订单数据
     *
     * @param request
     * @param orderHead
     * @return
     */
    public GridResult queryOrderListEq(GridRequest request, LogisticsOrderQdc orderHead) {
        String condition = " 1=1 ";
        if (StringUtils.isNotEmpty(orderHead.getOrderId())) {
            condition += " AND T_ORDER_HEAD_QDC.ORDER_ID='" + orderHead.getOrderId() + "'";
        }
        if (StringUtils.isNotEmpty(orderHead.getBuyerCertId())) {
            condition += " AND T_ORDER_HEAD_QDC.BUYER_CERT_ID='" + orderHead.getBuyerCertId() + "'";
        }
        if (StringUtils.isNotEmpty(orderHead.getWaybillId())) {
            condition += " AND T_WAYBILL_HEAD_QDC.WAYBILL_ID='" + orderHead.getWaybillId() + "'";
        }
//        if(StringUtils.isNotEmpty(orderHead.getOrgId())){
//            condition+=" AND T_ORDER_HEAD_QDC.Org_Id='"+orderHead.getOrgId()+"'";
//        }
        int count = orderHeadDao.countOrderEq(condition);
        List<LogisticsOrderQdc> orderHeadList = orderHeadDao.queryOrderEqList(
                request.getStart(), request.getEnd(), condition);
        return new GridResult(orderHeadList, request, count);
    }

    /**
     * 查询订单关联的所有商品数据
     *
     * @param request
     * @param relateId 关联订单唯一序列号
     * @return
     */
    public List<TGoodsListQdc> queryAllGoodsList(GridRequest request, String relateId) {
        TGoodsListQdc goodsListQdc = new TGoodsListQdc();
        goodsListQdc.setRelatedId(relateId);
        request.setSidx("gNo");
        request.setSord("asc");

        return goodsListDao.queryAll(TableConstants.T_GOODS_LIST_QDC, goodsListQdc,
                request.getSortOrder());
    }

    /**
     * 查询订单关联的所有运单数据
     *
     * @param request
     * @param relateId 关联订单唯一序列号
     * @return
     */
    public List<TWaybillListQdc> queryAllWaybillList(GridRequest request, String relateId) {
        TWaybillListQdc waybillListQdc = new TWaybillListQdc();
        waybillListQdc.setReleatedId(relateId);
        request.setSidx("waybillId");
        request.setSord("asc");

        return waybillListDao.queryAll(TableConstants.T_WAYBILL_LIST_QDC, waybillListQdc,
                request.getSortOrder());
    }

    /**
     * 查询运单关联的所有运单数据
     *
     * @param request
     * @param waybillId 关联订单唯一序列号
     * @return
     */
    public List<TWaybillListQdc> queryAllWaybillListByWarybillId(GridRequest request, String waybillId) {
        TWaybillListQdc waybillListQdc = new TWaybillListQdc();
        waybillListQdc.setWaybillId(waybillId);
        request.setSidx("waybillId");
        request.setSord("asc");

        return waybillListDao.queryAll(TableConstants.T_WAYBILL_LIST_QDC, waybillListQdc,
                request.getSortOrder());
    }

    /**
     * 查询运单关联的所有商品数据
     *
     * @param request
     * @param waybillId 运单号
     * @return
     */
    public List<TGoodsListQdc> queryAllGoodsListByWarybillId(GridRequest request, String waybillId) {
        List<TWaybillListQdc> waybillList = queryAllWaybillListByWarybillId(request, waybillId);
        if (!CollectionUtils.isEmpty(waybillList) && StringUtils.isNotEmpty(waybillList.get(0).getReleatedId())) {
            TGoodsListQdc goodsListQdc = new TGoodsListQdc();
            goodsListQdc.setRelatedId(waybillList.get(0).getReleatedId());
            request.setSidx("gNo");
            request.setSord("asc");

            return goodsListDao.queryAll(TableConstants.T_GOODS_LIST_QDC, goodsListQdc,
                    request.getSortOrder());
        } else {
            return new ArrayList<TGoodsListQdc>();
        }
    }
}
